package oop6.Task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Teacher01 {
    //使用slf4j
    public static final Logger Log = LoggerFactory.getLogger("oop12.PeopleTest");

    public static void main(String[] args) {    //处理异常
        Scanner input = new Scanner(System.in);
        if (Log.isDebugEnabled())
            Log.debug("请创建老师：");
        Teacher01 teacher = new Teacher01("男", "张三", "桃花小学");
        Log.info("请输入年龄：");
        try {
            teacher.setAge(input.nextInt());
        } catch (InputMismatchException e) {
            Log.warn("输入的不是数字", e);
        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage());
            Log.warn("年龄的设置有问题", e);
        }

        Log.info("这个老师的信息为：" + teacher);

    }

    private String sex;
    private String name;
    private String school;
    private int age;        // 像sex和name不应该提供set方法，可以提供get方法，外部不能改变这两个变量。age和school这两个属性可以提供set和get方法。

    public Teacher01(String sex, String name, String school) {
        this.sex = sex;
        this.name = name;
        this.school = school;
    }

    public Teacher01(String sex, String name, String school, int age) throws Exception {    //抛出异常
        this.sex = sex;
        this.name = name;
        this.school = school;
        if (age < 22)
            throw new Exception("这个年龄不合法," + age);
        this.age = age;
    }

    public void selfInfo() { //自我介绍
        System.out.println("我叫" + name + "，在" + school + "教书，年龄" + age + "岁");
    }

    public void giveLesson() {
        System.out.println("同学们，我们开始上课");
    }

    public String getSex() {
        return sex;
    }

    public String getName() {
        return name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws Exception {  //抛出异常
        if (age < 22)
            throw new Exception("这个年龄不合法," + age);
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher01{" +
                "sex=" + sex +
                ", name='" + name + '\'' +
                ", school='" + school + '\'' +
                ", age=" + age +
                '}';
    }
}